//
//  UserAPI.swift
//  DKE-Album
//
//  Created by 杨东宝 on 2020/6/4.
//  Copyright © 2020 杨东宝. All rights reserved.
//

import UIKit

import UIKit
import Moya
import KakaJSON

// 请求分类
public enum UserAPI {
    //MARK:-公共上传图片
    case userUpload(_ prefix_id:Int,_ images:[UIImage]) //公共上传图片,多张 prefix_id:存储到服务器上后会对应的文件夹（前缀）。1：用户提问 2 意见反馈
    //MARK:-个人资料相关
    case userBindQQ(_ dict:[String:Any]) //绑定QQ
    case userBindWX(_ dict:[String:Any]) //绑定微信
    case userUploadAvatar(_ image:UIImage) //修改头像
    case userModifyNickname(_ dict:[String:Any]) //修改昵称
    case userGetSchoolList(_ dict:[String:Any]) //搜索学校
    case userModifySchool(_ dict:[String:Any]) //设置学校
    case userSetBirthday(_ dict:[String:Any]) //修改生日
    case userSetSex(_ dict:[String:Any]) //修改生日
    case userAddAsk(_ data:[String:Any]) //提问题
    
    //MARK:-收藏相关
    case userGetFavoritedWord(_ data:[String:Any]) //全部收藏列表
    case userGetBookFavoritedWord(_ data:[String:Any]) //词本收藏列表
    case userAddFavoritedWord(_ data:[String:Any]) //收藏
    case userDelFavoritedWord(_ data:[String:Any]) //取消收藏
    //MARK:-错词本
    case userGetWrongWord(_ data:[String:Any]) //错词列表
    case userAddWrongWord(_ data:[String:Any]) //添加错词
    case userRemoveWrongWord(_ data:[String:Any]) //删除错词
    
    //MARK:-学习设置相关
    case userSetPushSwitch(_ switch:Int) //推送开关 0关闭1开启
    case userSetFavSwitch(_ switch:Int) //查询历史默认加入收藏 0关闭1开启
    case userSetPkSwitch(_ switch:Int) //PK赛提醒  0关闭1开启
    case userSetRemindTime(_ remind_time:String) //背单词提醒 提醒时间，例如：19:20。如果不再提醒，就设置为空字符串
    case userSetRemindSwitch(_ switch:Int) //背单词提醒开关
    case userGetStudySetting //获取学习设置
    //MARK:-获取客服电话
    case userGetCustomerServicePhone //获取客服电话
    //MARK:-我的学习档案
    case userGetStudyArchive(_ dict:[String:Any]) //我的学习档案
    //MARK:-意见反馈
     case userAddFeedback(_ dict:[String:Any]) //意见反馈
    //MARK:-我要提问
    case userAskQuestion(_ dict:[String:Any]) //我要提问
    
}
// 请求配置
extension UserAPI: TargetType {
    
    // 各个请求的具体路径
    public var path: String {
        switch self {
        case .userUpload:return "/user/uploads"
        case .userBindQQ:return "/user/bindQQ"
        case .userBindWX:return "/user/bindWX"
        case .userUploadAvatar:return "/user/uploadAvatar"
        case .userModifyNickname:return "/user/modifyNickname"
        case .userGetSchoolList: return "/user/getSchoolList"
        case .userModifySchool:return "/user/modifySchool"
        case .userSetBirthday:return "/user/setBirthday"
        case .userSetSex:return "/user/setSex"
        case .userAddAsk:return "/user/addAsk"
        case .userGetWrongWord:return "/user/getWrongWord"
        case .userAddWrongWord:return "/user/addWrongWord"
        case .userRemoveWrongWord:return "/user/removeWrongWord"
        case .userGetFavoritedWord:return "/user/getFavoritedWord"
        case .userGetBookFavoritedWord:return "/user/getBookFavoritedWord"
        case .userAddFavoritedWord:return "/user/addFavoritedWord"
        case .userDelFavoritedWord:return "/user/delFavoritedWord"
        case .userSetPushSwitch:return "/user/setPushSwitch"
        case .userSetFavSwitch:return "/user/setFavSwitch"
        case .userSetPkSwitch:return "/user/setPkSwitch"
        case .userSetRemindTime:return "/user/setRemindTime"
        case .userGetStudySetting:return "/user/getStudySetting"
        case .userSetRemindSwitch:return "/user/setRemindSwitch"
        case .userGetCustomerServicePhone:return "/user/getCustomerServicePhone"
        case .userGetStudyArchive:return "/user/getStudyArchive"
        case .userAddFeedback:return "/user/addFeedback"
        case .userAskQuestion:return "/user/askQuestion"
            
        }
    }
    // 请求类型
    public var method: Moya.Method {
        switch self {
        case .userUpload:
            return .post
        case .userBindQQ:
            return .post
        case .userBindWX:
            return .post
        case .userUploadAvatar:
            return .post
        case .userModifyNickname:
            return .post
        case .userGetSchoolList:
            return .post
        case .userModifySchool:
            return .post
        case .userSetBirthday:
            return .post
        case .userSetSex:
            return .post
        case .userAddAsk:
            return .post
        case .userGetWrongWord:
            return .get
        case .userAddWrongWord:
            return .get
        case .userRemoveWrongWord:
            return .post
        case .userGetFavoritedWord:
            return .get
        case .userGetBookFavoritedWord:
            return .get
        case .userAddFavoritedWord:
            return .post
        case .userDelFavoritedWord:
            return .post
        case .userSetPushSwitch:
            return .post
        case .userSetFavSwitch:
            return .post
        case .userSetPkSwitch:
            return .post
        case .userSetRemindTime:
            return .post
        case .userGetStudySetting:
            return .get
        case .userSetRemindSwitch:
            return .post
        case .userGetCustomerServicePhone:
            return .get
        case .userGetStudyArchive:
            return .get
        case .userAddFeedback:
             return .post
        case .userAskQuestion:
             return .post
         
        }
    }
    
    // 请求任务事件（这里附带上参数）
    public var task: Task {
        switch self {
        case .userUpload(let prefix_id ,let images):do {
            var dataArray:[Moya.MultipartFormData] = []
            for image in images {
                //图片转成Data
                let data:Data = image.jpegData(compressionQuality: 0.9)!
                //根据当前时间设置图片上传时候的名字
                let date:Date = Date()
                let formatter = DateFormatter()
                formatter.dateFormat = "yyyy-MM-dd-HH:mm:ss"
                var dateStr:String = formatter.string(from: date as Date)
                //别忘记这里给名字加上图片的后缀哦
                dateStr = dateStr.appendingFormat("-\(UUID().uuidString).png")
                
                let formData = MultipartFormData(provider: .data(data), name: "file[]", fileName: dateStr, mimeType: "image/jpeg")
                dataArray.append(formData)
            }
            return .uploadCompositeMultipart(dataArray, urlParameters: ["prefix_id":prefix_id])
        }
            
            
        case .userBindQQ(dict:let dict):
            return .requestParameters(parameters: dict, encoding: URLEncoding.default)
        case .userBindWX(dict: let dict):
            return .requestParameters(parameters: dict, encoding: URLEncoding.default)
            
            
        case .userUploadAvatar(image:let image):do {
            //图片转成Data
            let data:Data = image.jpegData(compressionQuality: 0.9)!
            //根据当前时间设置图片上传时候的名字
            let date:Date = Date()
            let formatter = DateFormatter()
            formatter.dateFormat = "yyyy-MM-dd-HH:mm:ss"
            var dateStr:String = formatter.string(from: date as Date)
            //别忘记这里给名字加上图片的后缀哦
            dateStr = dateStr.appendingFormat("-.png")
            
            let formData = MultipartFormData(provider: .data(data), name: "file", fileName: dateStr, mimeType: "image/jpeg")
            return .uploadMultipart([formData])
            
            }
        case .userModifyNickname(dict: let dict):
            return .requestParameters(parameters: dict, encoding: URLEncoding.default)
        case .userGetSchoolList(dict: let dict):
            return .requestParameters(parameters: dict, encoding: URLEncoding.default)
        case .userModifySchool(dict: let dict):
            return .requestParameters(parameters: dict, encoding: URLEncoding.default)
        case .userSetBirthday(dict: let dict):
            return .requestParameters(parameters: dict, encoding: URLEncoding.default)
        case .userSetSex(dict: let dict):
            return .requestParameters(parameters: dict, encoding: URLEncoding.default)
        case .userAddAsk(dict: let dict):
            return .requestParameters(parameters: dict, encoding: URLEncoding.default)
            
        case .userGetFavoritedWord(dict: let dict):
            return .requestParameters(parameters: dict, encoding: URLEncoding.default)
        case .userGetBookFavoritedWord(dict: let dict):
            return .requestParameters(parameters: dict, encoding: URLEncoding.default)
        case .userAddWrongWord(dict: let dict):
            return .requestParameters(parameters: dict, encoding: URLEncoding.default)
        case .userRemoveWrongWord(dict: let dict):
            return .requestParameters(parameters: dict, encoding: URLEncoding.default)
           
            
            
        case .userGetWrongWord(dict: let dict):
            return .requestParameters(parameters: dict, encoding: URLEncoding.default)
        case .userAddFavoritedWord(dict: let dict):
            return .requestParameters(parameters: dict, encoding: URLEncoding.default)
        case .userDelFavoritedWord(dict: let dict):
            return .requestParameters(parameters: dict, encoding: URLEncoding.default)
            
            
            
        case .userSetPushSwitch(switch:let SW):
            return .requestParameters(parameters: ["switch":SW], encoding: URLEncoding.default)
        case .userSetFavSwitch(switch:let SW):
            return .requestParameters(parameters: ["switch":SW], encoding: URLEncoding.default)
        case .userSetPkSwitch(switch:let SW):
            return .requestParameters(parameters: ["switch":SW], encoding: URLEncoding.default)
        case .userSetRemindSwitch(switch:let SW):
            return .requestParameters(parameters: ["switch":SW], encoding: URLEncoding.default)
        case .userSetRemindTime(time:let time):
            return .requestParameters(parameters: ["remind_time":time], encoding: URLEncoding.default)
        case .userGetStudySetting:
            return .requestPlain
        case .userGetCustomerServicePhone:
            return .requestPlain
        case .userGetStudyArchive(dict: let dict):
            return .requestParameters(parameters: dict, encoding: URLEncoding.default)
        case .userAddFeedback(dict: let dict):
            return .requestParameters(parameters: dict, encoding: URLEncoding.default)
        case .userAskQuestion(dict: let dict):
            return .requestParameters(parameters: dict, encoding: URLEncoding.default)
     
        }
        
    }
    // 是否执行Alamofire验证
    public var validate: Bool {
        return false
    }
    
}

